package com.spring.activemq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class ProducerServiceImpl implements IProducerService{
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Autowired
	private Destination queueDestination;
	
	public void sendMessage(final String message) {
		/*send方法有两个参数，一个是消息发送的目的地Destination，一个是用来创建消息的MessageCreator*/
		jmsTemplate.send(queueDestination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage= session.createTextMessage(message);
				System.out.println("发送的消息内容为："+textMessage.getText());
				return textMessage;
			}
		});
	}

}
